package com.xingdata.jjxc.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.graphics.Typeface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.adapter.CommentAdapter;
import com.xingdata.jjxc.base.Base1Activity;
import com.xingdata.jjxc.enty.CommentListEntity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.http.HttpUtil;
import com.xingdata.jjxc.utils.JUtils;
import com.xingdata.jjxc.utils.SystemInfo;

/**
 * 评论列表页面
 * 
 * @author Administrator
 * 
 */
public class CommentListView extends Base1Activity implements OnClickListener {
	private TextView title;
	private ListView CommentListView;
	private CommentAdapter adapter;
	private String dis_id;
	private List<CommentListEntity> listEntities = new ArrayList<CommentListEntity>();
	private EditText commentEditText;

	private void initdata() {
		dis_id = getIntent().getExtras().getString("dis_id");
		doPost_commentlist();
	}

	/**
	 * 评论列表请求
	 */
	private void doPost_commentlist() {
		paramsMap = new HashMap<String, String>();
		paramsMap.put("dis_id", dis_id);
		paramsMap.put("beginnum", "0");
		paramsMap.put("endnum", "1000");
		HttpUtil.getInstance(this).Post(App.ZZD_REQUEST_COMMENTLIST, paramsMap, new HttpResponseListener() {

			@Override
			public void onSuccess(RespEntity respEntity) {
				listEntities.addAll(JSON.parseArray(respEntity.getResult(), CommentListEntity.class));
				adapter.notifyDataSetChanged();
			}

			@Override
			public void onLoading(int vurrent) {

			}

			@Override
			public void onFailure(String result) {

			}
		});
	}

	/**
	 * 添加评论请求
	 */
	private void dopost_addcomment() {
		paramsMap = new HashMap<String, String>();
		paramsMap.put("userid", SystemInfo.getUserInfo().getUserid());
		paramsMap.put("access_token", SystemInfo.getUserInfo().getToken());
		paramsMap.put("dis_id", dis_id);
		paramsMap.put("comment_text", commentEditText.getText().toString());
		mHttpUtil.getInstance(this).Post(App.ZZD_REQUEST_ADDCOMMENT, paramsMap, new HttpResponseListener() {

			@Override
			public void onSuccess(RespEntity respEntity) {
				CommentListEntity commentListEntity = new CommentListEntity();
				commentListEntity.setComment_ctime("刚刚");
				commentListEntity.setUser_head(SystemInfo.getUserInfo().getHead());
				commentListEntity.setUser_name(SystemInfo.getUserInfo().getUsername());
				commentListEntity.setComment_text(commentEditText.getText().toString());
				listEntities.add(0, commentListEntity);
				adapter.notifyDataSetChanged();
				CommentListView.setSelection(0);
				showToast("评论成功");
				commentEditText.setText("");
				commentEditText.clearFocus();
			}

			@Override
			public void onLoading(int vurrent) {

			}

			@Override
			public void onFailure(String result) {

			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;
		case R.id.CommentWindow_bt:
			if (commentEditText.getText().toString().trim().length() == 0) {
				showToast("评论不能为空");
				return;
			}
			//
			dopost_addcomment();

			break;
		default:
			break;
		}
	}

	@Override
	protected int getContentViewId() {
		return R.layout.activity_comment;
	}

	@Override
	protected String getTitleText() {
		return JUtils.COMMENTS;
	}

	@Override
	protected void initViews() {
		title = (TextView) findViewById(R.id.title);
		title.setText("评论");
		((ImageView) findViewById(R.id.title_back)).setOnClickListener(this);
		commentEditText = (EditText) findViewById(R.id.comment_et);
		// 设置字体
		Typeface font = Typeface.createFromAsset(getAssets(), JUtils.TypefacePath);
		commentEditText.setTypeface(font);
		(findViewById(R.id.CommentWindow_bt)).setOnClickListener(this);
		CommentListView = (ListView) findViewById(R.id.CommentWindow);
		adapter = new CommentAdapter(CommentListView.this, listEntities, this);
		CommentListView.setAdapter(adapter);

		initdata();
	}
}
